package com.hxm.appGenealogy.config.tree;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hxm.appGenealogy.Function.BrothersSistersPeopleFunction;
import com.hxm.appGenealogy.Function.FamilyFunction;
import com.hxm.appGenealogy.Function.PeopleFunction;
import com.hxm.appGenealogy.Function.PeopleFunctionService;
import com.hxm.appGenealogy.config.Constant;
import com.hxm.appGenealogy.config.queryVo.QueryWrapperVO;
import com.hxm.appGenealogy.entity.BrothersSistersPeople;
import com.hxm.appGenealogy.entity.Family;
import com.hxm.appGenealogy.entity.People;
import com.hxm.appGenealogy.entity.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.MalformedParameterizedTypeException;
import java.util.*;

@Component
public class TreeConfig {


    @Autowired
    private PeopleFunction peopleFunction;


    @Autowired
    private PeopleFunctionService peopleFunctionService;


    @Autowired
    private BrothersSistersPeopleFunction brothersSistersPeopleFunction;

    @Autowired
    private FamilyFunction familyFunction;

    /**
     * 在添加父亲时,添加兄弟到父亲下(也就是说,父亲的儿女);
     * 前提是传入过来的兄弟的信息不能为空;
     * insertPeople 父亲的数据;
     */
    public void addSonOrGirl(People insertPeopleFather, List<BrothersSistersPeople> brothersSistersPeople) {
        for (BrothersSistersPeople brothersSistersPeoples : brothersSistersPeople) {
          /*  //获取兄弟的人员的信
            //获取到兄弟的信息;
            People people = peopleFunction.peopleGetByid(brothersSistersPeoples.getBrotherPeopleId());
            //判断兄弟的数据是不是为空;
            if (people == null) {
                continue;
            }
            PeopleService peopleServiceInsert = new PeopleService();
            //设置我的信息;
            peopleServiceInsert.setPeopleBelowId(people.getId());
            //设置男女;
            if (people.getGender().equals(Constant.genderboy)) {
                peopleServiceInsert.setType(Constant.genderboy);
            } else {
                peopleServiceInsert.setType(Constant.genderGirl);
            }
            people.setFatherPeopleId(insertPeopleFather.getId());
            //判断是否有妻子;
            if (insertPeopleFather.getWifePeopleId() != null) {
                //说明有妻子,说明这些孩子之前已经添加到母亲下面了,应为添加母亲的时候都会将这些孩子的数据添加到母亲的下面,
                //查询出来母亲添加过这个人的数据;
                PeopleService peopleServices = peopleFunctionService.getQueryOne(JSON.toJSONString(new QueryWrapperVO()
                        .eq("people_monther_id", insertPeopleFather.getWifePeopleId())
                        .eq("people_below_id", brothersSistersPeoples.getBrotherPeopleId())
                ));
                if (peopleServices != null) {
                    peopleServices.setPeopleFatherId(insertPeopleFather.getId());
                    peopleFunctionService.updateByid(peopleServices);
                } else {
                    //说明之前没有添加过;
                    peopleServiceInsert.setPeopleMontherId(insertPeopleFather.getWifePeopleId());
                    peopleServiceInsert.setPeopleFatherId(insertPeopleFather.getId());
                    peopleFunctionService.insertPeople(peopleServiceInsert);
                    people.setMotherPeopleId(insertPeopleFather.getWifePeopleId());
                }
            } else {
                //说明没有妻子;
                PeopleService peopleServices = peopleFunctionService.getQueryOne(JSON.toJSONString(new QueryWrapperVO()
                        .eq("people_father_id", insertPeopleFather.getId())
                        .eq("people_below_id", people.getId())
                ));
                if (peopleServices == null) {
                    //说明我的父亲没有妻子;
                    //设置我父亲的信息;
                    peopleServiceInsert.setPeopleFatherId(insertPeopleFather.getId());
                    peopleFunctionService.insertPeople(peopleServiceInsert);

                }
            }
            peopleFunction.updateByid(people);
        }*/
            //去重复教验;
            //去重复
            PeopleService queryOne = peopleFunctionService.getQueryOne(JSON.toJSONString(new QueryWrapperVO()
                    .eq("people_father_id", insertPeopleFather.getId())
                    .eq("people_below_id", brothersSistersPeoples.getBrotherPeopleId())
            ));
            if(queryOne!=null){
                continue;
            }
            PeopleService queryOne2 = peopleFunctionService.getQueryOne(JSON.toJSONString(new QueryWrapperVO()
                    .eq("people_monther_id", insertPeopleFather.getWifePeopleId())
                    .eq("people_below_id", brothersSistersPeoples.getBrotherPeopleId())
            ));
            if(queryOne2!=null){
                queryOne2.setPeopleFatherId(insertPeopleFather.getId());
                peopleFunctionService.updateByid(queryOne2);
            }
            Long brotherPeopleId = brothersSistersPeoples.getBrotherPeopleId();
            People people = peopleFunction.peopleGetByid(brotherPeopleId);
            Integer gender = people.getGender();
            PeopleService peopleService = new PeopleService();
            peopleService.setPeopleBelowId(brotherPeopleId);
            peopleService.setType(gender);
            peopleService.setPeopleFatherId(insertPeopleFather.getId());
            if(insertPeopleFather.getWifePeopleId()!=null){
                peopleService.setPeopleMontherId(insertPeopleFather.getWifePeopleId());
            }
            peopleFunctionService.insertPeople(peopleService);
        }
    }
    /**
     * 将一个人添加到父亲的下面去;
     */
    public  void addSonOrGirlOne(People peopleSon,People peopleFather){
        PeopleService peopleService = new PeopleService();
        peopleService.setPeopleFatherId(peopleFather.getId());
        if(peopleFather.getWifePeopleId()!=null){
            peopleService.setPeopleMontherId(peopleFather.getWifePeopleId());
        }
        peopleService.setType(peopleSon.getGender());
        peopleService.setPeopleBelowId(peopleSon.getId());
        peopleFunctionService.insertPeople(peopleService);
    }

    /**
     * 判断我是不是妻子;
     */
    public Boolean isWife(People people){
        People queryOne = peopleFunction.queryGetOneString(JSON.toJSONString(new QueryWrapperVO()
                .eq("wife_people_id",people.getId())
        ));
        if(queryOne!=null){
            return true;
        }else {
            return false;
        }
    }


    /**
     * 添加母亲时判断我有没有兄弟姐妹;
     * @param insertPeopleFather
     * @param brothersSistersPeople
     */
    public void addSonOrGirsMonther(People insertPeopleFather, List<BrothersSistersPeople> brothersSistersPeople) {
        for (BrothersSistersPeople brothersSistersPeoples : brothersSistersPeople) {
            //获取兄弟的人员的信
            //获取到兄弟的信息;
            People people = peopleFunction.peopleGetByid(brothersSistersPeoples.getBrotherPeopleId());
            //判断兄弟的数据是不是为空;
            if (people == null) {
                continue;
            }
            //说明我的父亲没有妻子;
            //只是添加我的兄弟们到我的下面即可;
            PeopleService peopleServiceInsert = new PeopleService();
            //设置我的信息;
            peopleServiceInsert.setPeopleBelowId(people.getId());
            //设置男女;
            if (people.getGender().equals(Constant.genderboy)) {
                peopleServiceInsert.setType(Constant.genderboy);
            } else {
                peopleServiceInsert.setType(Constant.genderGirl);
            }
            //设置兄弟母亲的信息;
            people.setMotherPeopleId(insertPeopleFather.getId());
            //判断是否有丈夫;
            if (insertPeopleFather.getHusbandPeopleId() != null) {
                //说明有妻子,说明这些孩子之前已经添加到母亲下面了,应为添加母亲的时候都会将这些孩子的数据添加到母亲的下面,
                //查询出来父亲添加过这些孩子的数据;
                PeopleService peopleServices = peopleFunctionService.getQueryOne(JSON.toJSONString(new QueryWrapperVO().eq("people_father_id", insertPeopleFather.getWifePeopleId()).eq("people_below_id", brothersSistersPeoples.getBrotherPeopleId())));
                //遍历我父亲之前绑定的这些孩子的信息,设置上我父亲的数据的信息;
                if (peopleServices != null) {
                    peopleServices.setPeopleMontherId(insertPeopleFather.getId());
                    peopleFunctionService.updateByid(peopleServices);
                } else {
                    peopleServiceInsert.setPeopleMontherId(insertPeopleFather.getId());
                    peopleServiceInsert.setPeopleFatherId(insertPeopleFather.getHusbandPeopleId());
                    peopleFunctionService.insertPeople(peopleServiceInsert);
                    people.setFatherPeopleId(insertPeopleFather.getHusbandPeopleId());
                }
            } else {
                PeopleService peopleServices = peopleFunctionService.getQueryOne(JSON.toJSONString(new QueryWrapperVO()
                        .eq("people_monther_id", insertPeopleFather.getId())
                        .eq("people_below_id", people.getId())
                ));
                if (peopleServices == null) {
                    //设置我母亲的信息;
                    peopleServiceInsert.setPeopleMontherId(insertPeopleFather.getId());
                    peopleFunctionService.insertPeople(peopleServiceInsert);
                }
            }
            peopleFunction.updateByid(people);
        }
    }

    /**
     * 我的兄弟,和我新添加的兄弟做一个双向的绑定;
     *
     * @param people
     * @param brothersSistersPeopleList
     */
    public void addBrotherSistersAdd(People people, List<BrothersSistersPeople> brothersSistersPeopleList) {
        for (BrothersSistersPeople brothersSistersPeople : brothersSistersPeopleList) {
            //获取我兄弟的信息;
            Long brotherPeopleId = brothersSistersPeople.getBrotherPeopleId();
            //获取我的id;
            Long id = people.getId();
            //双向绑定相互添加;
            BrothersSistersPeople brothersSistersPeople1 = new BrothersSistersPeople();
            brothersSistersPeople1.setMyPeopleId(brotherPeopleId);
            brothersSistersPeople1.setBrotherPeopleId(id);
            brothersSistersPeopleFunction.insert(brothersSistersPeople1);
            //双向绑定,相互添加;
            BrothersSistersPeople brothersSistersPeople2 = new BrothersSistersPeople();
            brothersSistersPeople2.setBrotherPeopleId(brotherPeopleId);
            brothersSistersPeople2.setMyPeopleId(id);
            brothersSistersPeopleFunction.insert(brothersSistersPeople2);
        }
    }


    /**
     * 我的兄弟,和我新添加的兄弟做一个双向的绑定;
     *
     * @param people
     * @param
     */
    public void addBrotherPeopleAndMyPeople(People people, People peopleBrother) {
        //获取我兄弟的信息;
        Long brotherPeopleId = peopleBrother.getId();
        //获取我的id;
        Long id = people.getId();
        //双向绑定相互添加;
        BrothersSistersPeople brothersSistersPeople1 = new BrothersSistersPeople();
        brothersSistersPeople1.setMyPeopleId(brotherPeopleId);
        brothersSistersPeople1.setBrotherPeopleId(id);
        brothersSistersPeopleFunction.insert(brothersSistersPeople1);
        //双向绑定,相互添加;
        BrothersSistersPeople brothersSistersPeople2 = new BrothersSistersPeople();
        brothersSistersPeople2.setBrotherPeopleId(brotherPeopleId);
        brothersSistersPeople2.setMyPeopleId(id);
        brothersSistersPeopleFunction.insert(brothersSistersPeople2);
    }

    /**
     * 将儿子们的信息添加到妻子或者是丈夫下下;
     * type 0是添加父亲的数据,1是添加母亲的数据;
     */
    public void addSon(People people, List<PeopleService> peopleServiceList, Integer type) {
        for (PeopleService peopleService : peopleServiceList) {
            Long peopleBelowId = peopleService.getPeopleBelowId();
            People peopleSon = peopleFunction.peopleGetByid(peopleBelowId);
            if (type == 0) {
                peopleService.setPeopleFatherId(people.getId());
                peopleSon.setFatherPeopleId(people.getId());

            } else {
                peopleSon.setMotherPeopleId(people.getId());
                peopleService.setPeopleMontherId(people.getId());
            }
            peopleFunction.updateByid(peopleSon);
            peopleFunctionService.updateByid(peopleService);
        }
    }


    /**
     * 添加儿子或者是女儿;
     */
    public void addSonOrGirl(People peopleFatherOrMonther, People peopleInsertBoyOrGirl) {
        PeopleService peopleService = new PeopleService();
        peopleService.setType(peopleInsertBoyOrGirl.getGender());
        peopleService.setPeopleBelowId(peopleInsertBoyOrGirl.getId());
        //说明是父亲;
        Integer gender = peopleFatherOrMonther.getGender();
        if (gender.equals(Constant.genderboy)) {
            peopleService.setPeopleFatherId(peopleFatherOrMonther.getId());
            if (peopleFatherOrMonther.getWifePeopleId() != null) {
                peopleService.setPeopleMontherId(peopleFatherOrMonther.getWifePeopleId());
            }
        } else {
            //说明是母亲;
            peopleService.setPeopleMontherId(peopleFatherOrMonther.getId());
            if (peopleFatherOrMonther.getHusbandPeopleId() != null) {
                peopleService.setPeopleFatherId(peopleFatherOrMonther.getHusbandPeopleId());
            }
        }
        peopleFunctionService.insertPeople(peopleService);
    }

    /**
     * 将新添加的儿子或者是女儿添加到其母亲下,并且和下面的孩子做一个想哭绑定的关系;
     */
    public void addBrotherToFastherOrMontherSonGo(People peopleFatherOrMonther, People peopleInsertBoyOrGirl) {
        List<PeopleService> peopleFatherService = null;
        if (peopleFatherOrMonther.getGender().equals(Constant.genderboy)) {
            //说明是父亲;
            //查询父亲下的儿子的信息;
            if (peopleFatherOrMonther.getWifePeopleId() != null) {
                //说明有母亲;
                peopleFatherService = peopleFunctionService.slectStringLamQuery(JSON.toJSONString(new QueryWrapperVO()
                        .eq("people_father_id", peopleFatherOrMonther.getId())
                        .eq("people_monther_id", peopleFatherOrMonther.getWifePeopleId())
                ));
            } else {
                peopleFatherService = peopleFunctionService.slectStringLamQuery(JSON.toJSONString(new QueryWrapperVO()
                        .eq("people_father_id", peopleFatherOrMonther.getId())
                ));
            }
            //说明没有母亲
            for (PeopleService peopleService : peopleFatherService) {
                People peopleGetByid = peopleFunction.peopleGetByid(peopleService.getPeopleBelowId());
                addBrotherPeopleAndMyPeople(peopleInsertBoyOrGirl, peopleGetByid);
            }
        } else {
            //说明是母亲;
            if (peopleFatherOrMonther.getHusbandPeopleId() != null) {
                peopleFatherService = peopleFunctionService.slectStringLamQuery(JSON.toJSONString(new QueryWrapperVO()
                        .eq("people_father_id", peopleFatherOrMonther.getId())
                        .eq("people_monther_id", peopleFatherOrMonther.getWifePeopleId())
                ));
            } else {
                peopleFatherService = peopleFunctionService.slectStringLamQuery(JSON.toJSONString(new QueryWrapperVO()
                        .eq("people_father_id", peopleFatherOrMonther.getId())
                ));
            }
        }
    }

    /**
     * 添加家族最顶级的人的数据;
     */
    public void addTop(People people, Family family){
        family.setTopPeopleId(people.getId());
        familyFunction.updateById(family);
    }

  /*  *
     *查询家族
     *@return
     */
    public People selectPeopleTop(Long peopleTopId){
        //有两种可能性;
        People people = peopleFunction.peopleGetByid(peopleTopId);
        return people;
    }

    /**
     * 查询出来我所有的兄弟姐妹的人员信息;
     * @param people
     * @return
     */
    public List<People> brothersSistersPeopleList(People people){
        Long id = people.getId();
        List<BrothersSistersPeople> brothersSistersPeopleList = brothersSistersPeopleFunction.slectStringQuery(JSON.toJSONString(
                new QueryWrapperVO().eq("my_people_id", id)
        ));
        Long wifePeopleId = people.getWifePeopleId();
        //说明我有妻子;
        List<BrothersSistersPeople> brothersSistersPeopleListWfile =null;
        if(people.getWifePeopleId()!=null){
            brothersSistersPeopleListWfile = brothersSistersPeopleFunction.slectStringQuery(JSON.toJSONString(
                    new QueryWrapperVO().eq("my_people_id",people.getWifePeopleId())
            ));
        }

        List<People> peopleList = new LinkedList<>();
        //将我的信息也添加进去;
        peopleList.add(people);
        //遍历;
        if(brothersSistersPeopleList.size()>0){
            for (BrothersSistersPeople brothersSistersPeople : brothersSistersPeopleList) {
                Long brotherPeopleId = brothersSistersPeople.getBrotherPeopleId();
                People peopleBrother = peopleFunction.peopleGetByid(brotherPeopleId);
                peopleList.add(peopleBrother);
            }
        }
        List<People> peopleWife = new ArrayList<>();
        if(brothersSistersPeopleListWfile!=null){
            if(brothersSistersPeopleListWfile.size()>0 ){
                for (BrothersSistersPeople brothersSistersPeople : brothersSistersPeopleListWfile) {
                    Long brotherPeopleId = brothersSistersPeople.getBrotherPeopleId();
                    People peopleBrother = peopleFunction.peopleGetByid(brotherPeopleId);
                    for (People people1 : peopleList) {
                        if(people1.getId().equals(peopleBrother.getId())){
                            continue;
                        }else {
                            peopleWife.add(peopleBrother);
                        }
                    }
                }
            }
        }
        for (People people1 : peopleWife) {
            peopleList.add(people1);
        }

        return peopleList;
    }

    /**
     * 查询出来我孩子的信息的数据;
     * 1是男，2是女;
     */
    public List<People> peopleGetSonOrgirl(People people,Integer type){
        QueryWrapperVO queryWrapperVO = new QueryWrapperVO();
        if(type==1){
            queryWrapperVO.eq("people_father_id",people.getId());
        }else {
            queryWrapperVO.eq("people_monther_id",people.getId());
        }
        List<People> peopleList = new ArrayList<>();
        List<PeopleService> peopleServices = peopleFunctionService.slectStringLamQuery(JSON.toJSONString(queryWrapperVO));
        for (PeopleService peopleService : peopleServices) {
            Long peopleBelowId = peopleService.getPeopleBelowId();
            People peopleGetByid = peopleFunction.peopleGetByid(peopleBelowId);
            if(peopleGetByid!=null){
                peopleList.add(peopleGetByid);
            }
        }
        return peopleList;
    }


    /**
     * 查询妻子的信息;
     */
    public People getWifeList(People people){
        People wifePeopleId = peopleFunction.queryGetOneString(JSON.toJSONString(new QueryWrapperVO()
                .eq("wife_people_id", people.getWifePeopleId())
        ));
        People people1 = peopleFunction.peopleGetByid(wifePeopleId.getId());
        return  people1;
    }

}















